package com.doudoukeji.service.Activity.Login;

import android.content.Intent;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.doudoukeji.service.Activity.disclaimer.DisclaimerActivity;
import com.doudoukeji.service.Activity.mvp.MainActivity;
import com.doudoukeji.service.Activity.myhospital.EditMessageActivity;
import com.doudoukeji.service.Dialog.LoadingDialog;
import com.doudoukeji.service.R;
import com.doudoukeji.service.Utils.MyToast;
import com.doudoukeji.service.Utils.SharedPreferencesUtils;
import com.doudoukeji.service.Utils.Util;
import com.sanhai.android.mvp.EduActivity;

import org.jivesoftware.smack.util.StringUtils;


/**
 * @version 2.0
 * @类描述： 登陆页
 * @项目名称：ddgh
 * @类名称： loginActivity
 * @包名称： com.doudoukeji.service.Activity.login
 * @创建人：张强 boy
 * @创建时间：2016/3/7 16:40
 * @修改人：
 * @修改时间：2016/3/7 16:40
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class loginActivity extends EduActivity implements
        OnFocusChangeListener, LoginView {
    // topbar部分
    private Button mBtnBack;
    private TextView mTvTitle;

    // 本界面
    private EditText mEtPhone, mEtCode, mEtInvitCode;

    //EditText文本项集合
    private EditText[] mEdittexts;
    private Button mBtnVerification, mBtnConfirm;
    private String phoneNum = "", checkCode = "", InviteCode = "";

    // 进度条
    private LoadingDialog loading;

    //请求接口
    private LoginPresenter presenter;
    private MyCount myCount;
    private boolean isStart = false;
    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_login);
    }


    @Override
    public void init() {
        //初始化控件
        initView();
        //初始化操作
        initData();
    }

    /**
     * s初始化操作
     */
    private void initData() {
        //初始化presenter
        presenter = new LoginPresenter(this, this);

        //设置文本变化监听器(手机号，验证码，邀请码)
        for (int x = 0; x < mEdittexts.length; x++) {
            setEtInvitTextChangeListener(mEdittexts[x]);
        }


    }

    /**
     * 设置邀请码文本变化监听器
     */
    private void setEtInvitTextChangeListener(EditText mEt) {

        mEt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                          int arg3) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (mEtCode.getText().length() == 4 && mEtPhone.getText().length() == 11) {
                    mBtnConfirm.setClickable(true);
                    mBtnConfirm.setEnabled(true);
                    mBtnConfirm
                            .setBackgroundResource(R.drawable.btn_home_selector);
                } else {
                    mBtnConfirm.setClickable(false);
                    mBtnConfirm.setEnabled(false);
                    mBtnConfirm
                            .setBackgroundResource(R.drawable.btn_code_normal);
                }

                if (mEtPhone.getText().length() == 11) {
                    mBtnVerification.setClickable(true);
                    mBtnVerification.setEnabled(true);
                    mBtnVerification
                            .setBackgroundResource(R.drawable.btn_home_selector);
                } else {
                    mBtnVerification.setClickable(false);
                    mBtnVerification.setEnabled(false);
                    mBtnVerification
                            .setBackgroundResource(R.drawable.btn_code_normal);
                }

                if (isStart){
                    mBtnVerification.setEnabled(false);
                }

            }
        });
    }


    /**
     * 初始化控件
     */
    private void initView() {

        // topbar部分
        mBtnBack = (Button) findViewById(R.id.btn_back);
        mBtnBack.setOnClickListener(this);
        mBtnBack.setVisibility(View.GONE);
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvTitle.setText("登录");
        mTvTitle.setOnClickListener(this);
        // 本界面
        //手机号
        mEtPhone = (EditText) findViewById(R.id.et_phone);
        mEtPhone.setOnFocusChangeListener(this);
        //验证码
        mEtCode = (EditText) findViewById(R.id.et_code);
        mEtCode.setOnFocusChangeListener(this);
        //邀请码
        mEtInvitCode = (EditText) findViewById(R.id.et_invitation_code);
        mEtInvitCode.setOnFocusChangeListener(this);
        //验证按钮
        mBtnVerification = (Button) findViewById(R.id.btn_verification);
        mBtnVerification.setOnClickListener(this);
        mBtnVerification.setEnabled(false);
        //登录按钮
        mBtnConfirm = (Button) findViewById(R.id.btn_confirm);
        mBtnConfirm.setOnClickListener(this);
        mBtnConfirm.setEnabled(false);
        //文本集合
        mEdittexts = new EditText[]{mEtPhone, mEtCode, mEtInvitCode};
    }

    @Override
    public void onClickEvent(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                loginActivity.this.finish();
                break;
            case R.id.btn_verification:
                if (codeCheckValue()) {
                    presenter.getInvitationCode(phoneNum);
                }
                break;
            case R.id.btn_confirm:
                if (isCheckOk()) {
                    presenter.login(phoneNum, checkCode, InviteCode);
                }


                break;
            default:
                break;
        }
    }

    /**
     * 聚焦状态变化情况
     *
     * @param v
     * @param hasFocus
     */
    // TODO 全粘贴
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        switch (v.getId()) {
            case R.id.et_phone:
                mEtPhone.setBackgroundResource(R.drawable.et_login_orange);
                mEtCode.setBackgroundResource(R.drawable.et_login_wihte);
                mEtInvitCode.setBackgroundResource(R.drawable.et_login_wihte);
                break;
            case R.id.et_code:
                mEtPhone.setBackgroundResource(R.drawable.et_login_wihte);
                mEtCode.setBackgroundResource(R.drawable.et_login_orange);
                mEtInvitCode.setBackgroundResource(R.drawable.et_login_wihte);
                break;
            case R.id.et_invitation_code:
                mEtPhone.setBackgroundResource(R.drawable.et_login_wihte);
                mEtCode.setBackgroundResource(R.drawable.et_login_wihte);
                mEtInvitCode.setBackgroundResource(R.drawable.et_login_orange);

            default:
                break;
        }

    }

    /**
     * 文本项检测
     *
     * @return
     */
    private Boolean isCheckOk() {
        phoneNum = mEtPhone.getText().toString().trim();
        checkCode = mEtCode.getText().toString().trim();
        InviteCode = mEtInvitCode.getText().toString().trim();
        if (!"".equals(phoneNum) && phoneNum !=null) {
            MyToast.DefaultmakeText(loginActivity.this, "请输入手机号", 0).show();
            return false;
        }
        if (!"".equals(checkCode) && checkCode !=null) {
            MyToast.DefaultmakeText(loginActivity.this, "请输入验证码", 0).show();
            return false;
        }
//        if (!StringUtils.isNotBlank(InviteCode)) {
//            MyToast.DefaultmakeText(loginActivity.this, "请填写邀请码", 0).show();
//            return false;
//        }
        return true;

    }


    @Override
    public void getInvitationCode() {
            mBtnVerification.setEnabled(false);
            myCount = new MyCount(60000, 1000);
            myCount.start();// 请求服务器发送验证码
    }

    @Override
    public void intentEditMessageActivity() {
        IntentActivity(EditMessageActivity.class);
    }

    /**
     * 跳转界面
     * @param Activityclass
     */
    private void  IntentActivity  (Class Activityclass) {
        Intent intent = new Intent(this, Activityclass);
        startActivity(intent);
        finish();
    }

    @Override
    public void intentMainActivity() {
        IntentActivity(MainActivity.class);
    }

    @Override
    public void initentDisclaimerActivity() {
        IntentActivity(DisclaimerActivity.class);
    }


    public class MyCount extends CountDownTimer {
        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            isStart = true;
        }

        @Override
        public void onFinish() {
            mBtnVerification.setText("获取验证码");
            // sendCode.setBackgroundResource(R.drawable.date_btn_no);
            // sendCode.setTextColor(Color.parseColor("#00923f"));
            mBtnVerification.setEnabled(true);
            isStart=false;
        }

        @Override
        public void onTick(long millisUntilFinished) {
            mBtnVerification.setText(millisUntilFinished / 1000+"秒");
            mBtnVerification.setTextColor(Color.parseColor("#ffffff"));
        }
    }

    private Boolean codeCheckValue() {
        phoneNum = mEtPhone.getText().toString().trim();
        if (!Util.isMobileNo(phoneNum)) {
            MyToast.DefaultmakeText(loginActivity.this, "请输入正确格式的手机号", 0)
                    .show();
            return false;
        } else {
            return true;
        }

    }

}
